import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'
Vue.use(Router)

import nav from './nav.js'
import common from './common.js'
var router= new Router({
  mode:'hash',//hash,//history
  base:process.env.BASE_URL,
  routes: [
    ...nav,
    ...common
  ]
})

//路由拦截
router.beforeEach((to, from, next) => {
 next()
  // var hasLogin=localStorage.getItem("user");
  // if(!hasLogin && to.path!=="/login"){
  //   next( { path:'/login' } )
  // }else{
  //   let permission=store.state.permission
  //   let resultList=[]
  //   travers(permission,to.name,resultList)
  //   //对首页三个nav进行路由权限拦截
  //   if(to.name==="home" || to.name==="onepage"){
  //     if(resultList.length===0){
  //       router.push("/index/no-permission")
  //       return
  //     }
  //   }
  //   next()
  // }

})

//对象遍历查询
function travers(obj,name,resultList){
  if(!obj || !obj instanceof Object){
    return
  }
  if(Object.prototype.toString.call(obj)==="[object Array]"){
      if(obj.length===0){
          return
      }
      obj.forEach((item)=>{
          travers(item,name,resultList)
      })
  }else if(Object.prototype.toString.call(obj)==="[object Object]"){
      if(obj.label===name){
        if(obj.permission===true){
          resultList.push(obj)
        }
        return
      }
      if(obj.children && obj.children instanceof Array){
          obj.children.forEach((item)=>{
              travers(item,name,resultList)
          })
        }else{
            return
        }
  }
}



export default router

